<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { Menu as IconMenu, User, Tools, Setting, HomeFilled, ArrowRight, MapLocation, Document, Odometer, Warning } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

// 控制侧边栏折叠状态
const isCollapse = ref(false)
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

// 获取用户信息
const userInfo = computed(() => userStore.userInfo)

// 登出处理
const handleLogout = () => {
  ElMessage.success('已安全退出系统')
  userStore.logout()
}

// 根据用户角色显示不同的菜单项
const menuItems = computed(() => {
  const items = []

  if (userInfo.value.role === 'admin') {
    items.push(
      {
        index: '/admin',
        title: '系统管理',
        icon: Setting,
        roles: ['admin']
      },
      {
        index: '/admin/road-damage',
        title: '道路损坏管理',
        icon: Warning,
        roles: ['admin']
      }
    )
  }

  if (userInfo.value.role === 'maintainer') {
    items.push(
      {
        index: '/maintenance',
        title: '养护工作',
        icon: Tools,
        roles: ['maintainer']
      },
      {
        index: '/maintenance/road-damage',
        title: '道路损坏处理',
        icon: Warning,
        roles: ['maintainer']
      },
      {
        index: '/maintenance/reports',
        title: '工作报告',
        icon: Document,
        roles: ['maintainer']
      }
    )
  }

  if (userInfo.value.role === 'user') {
    items.push(
      {
        index: '/user',
        title: '道路信息',
        icon: Document,
        roles: ['user']
      },
      {
        index: '/user/report-damage',
        title: '道路损坏上报',
        icon: Warning,
        roles: ['user']
      }
    )
  }

  return items.filter(item => item.roles.includes(userInfo.value.role))
})

// 检查用户登录状态
onMounted(() => {
  if (!userStore.isLoggedIn && !userStore.isVisitor) {
    router.push('/login')
  } else if (userStore.isVisitor) {
    router.push('/welcome')
  }
})
</script>

<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside>
      <div class="sidebar-header">
        <img src="../assets/logo.svg" alt="Logo" class="sidebar-logo" />
        <span v-if="!isCollapse" class="sidebar-title">道路养护系统</span>
      </div>

      <el-menu
        :collapse="isCollapse"
        :default-active="$route.path"
        class="sidebar-menu"
        router
        background-color="#001529"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-menu-item v-for="item in menuItems" :key="item.index" :index="item.index">
          <el-icon><component :is="item.icon" /></el-icon>
          <template #title>{{ item.title }}</template>
        </el-menu-item>
      </el-menu>

      <div class="sidebar-footer" @click="toggleSidebar">
        <el-icon><arrow-right :class="{ 'is-rotate': !isCollapse }" /></el-icon>
      </div>
    </el-aside>

    <el-container>
      <!-- 顶部导航栏 -->
      <el-header>
        <div class="header-left">
          <el-icon class="menu-icon" @click="toggleSidebar"><icon-menu /></el-icon>
          <span class="header-title">道路养护管理系统</span>
        </div>

        <div class="header-right">
          <el-dropdown trigger="click">
            <div class="user-info">
              <el-avatar :icon="User" size="small" />
              <span class="username">{{ userInfo.username }}</span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>
                  <span>角色: {{
                    userInfo.role === 'admin' ? '管理员' :
                    userInfo.role === 'maintainer' ? '养护人员' :
                    userInfo.role === 'user' ? '普通用户' : '未知角色'
                  }}</span>
                </el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主要内容区域 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  color: #333;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0 20px;
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
}

.menu-icon {
  font-size: 20px;
  cursor: pointer;
  margin-right: 15px;
}

.header-title {
  font-size: 18px;
  font-weight: bold;
  color: #409EFF;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  font-size: 14px;
}

.el-aside {
  background-color: #001529;
  color: #fff;
  transition: width 0.3s;
  overflow: hidden;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  overflow: hidden;
}

.sidebar-logo {
  width: 32px;
  height: 32px;
}

.sidebar-title {
  margin-left: 10px;
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  white-space: nowrap;
}

.sidebar-menu {
  border-right: none;
}

.sidebar-footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  background-color: rgba(0, 0, 0, 0.2);
}

.is-rotate {
  transform: rotate(180deg);
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style>
